# -*- coding:UTF-8 -*-#

'''
@author: qianwp
'''
from popcan.core.structures import Singleton
from popcan.helper.loggerhelper import QueueLogger,Module
import traceback

class IHalt(object):
    
    def run(self):
        pass
    
class HaltManager(Singleton):
    
    def __init__(self):
        self.hookers = []
        
    def addHooker(self,hooker):
        self.hookers.append(hooker)
        
    def run(self):
        QueueLogger().warn(Module.Halt, 'shutdown hook is running', traceback.format_exc())
        for hooker in self.hookers:
            hooker.run()
        QueueLogger().shutdown()
        
class PooldbHaltHooker(IHalt):
    
    def __init__(self,pooldb):
        self.pooldb = pooldb
    
    def run(self):
        self.pooldb.close()
        
class MemcacheHaltHooker(IHalt):
    
    def __init__(self,linker):
        self.linker = linker
        
    def run(self):
        self.linker.close()
        
class RedisHaltHooker(IHalt):
    
    def __init__(self,linker):
        self.linker = linker
        
    def run(self):
        self.linker.close()